package model.factories.misc;

import java.util.Map;
import java.util.Scanner;
import java.util.regex.Pattern;
import model.Interpreter;
import model.SyntaxErrorException;
import model.commands.Command;
import model.commands.misc.MakeCommand;
import model.factories.CommandFactory;


/**
 * A factory used to build MakeCommand objects.
 * 
 * @author Erick Gonzalez
 * @author JLongley
 */
public class MakeCommandFactory extends CommandFactory {

    private static final String INVALID_VARIABLE_SYNTAX =
            "Defined variables must start with a colon and only contain alphabetic characters";
    private final Pattern myMakePattern = Pattern.
            compile(getInterpreter().getLanguage().getString("MakeCommandPattern"));
    
    

    /**
     * 
     * @param interpreter an Interpreter
     */
    public MakeCommandFactory(Interpreter interpreter) {
        super(interpreter);
    }

    @Override
    public Command createCommand(Scanner commandReader,
                                 Map<String, Integer> variables) throws SyntaxErrorException {
        Interpreter interpreter = getInterpreter();

        // remove command keyword from commandReader
        commandReader.next();

        // if variable doesn't match the prescribed pattern
        if (!commandReader.hasNext(VariableCommandFactory.VARIABLE_PATTERN)) { 
            throw new SyntaxErrorException(INVALID_VARIABLE_SYNTAX); 
        }

        // get name of variable and test validity
        String varName = commandReader.next();

        Command makeCommand = new MakeCommand(variables, varName);

        Command arg = interpreter.parse(commandReader, variables);
        makeCommand.getArguments().add(arg);

        return makeCommand;
    }

    @Override
    public Pattern patternToMatch() {
        return myMakePattern;
    }
}
